<?php

class CommentsController extends \Phalcon\Mvc\Controller
{

    public function indexAction()
    {

    }

    public function insertAction()
    {
    	$result = array();
        
    	if($this->request->isPost())
    	{
    		$comments = nl2br($_POST['comments']);
    		$feed_id = $_POST['feed_id'];
    		$receiver_id = $_POST['receiver_id'];
    		$sender_id = $_POST['sender_id'];
            $data = array(
                        "feeds_id"=>$feed_id,
                        "user_id"=>$sender_id,
                        "receiver_id"=>$receiver_id,
                        "comment_container"=>htmlentities($comments)
                    );
            $comment = new FeedsCommentsTb();
            if($comment->save($data) == false)
            {
                $messages = '';
                foreach ($comment->getMessages() as  $value) {
                    $messages .= $value."<br>";
                }
                $result['messages'] = $messages;
                $result['status'] = false;
            }
            else
            {
                $data['comment_id'] = $comment->id;
                $data['date_created'] = $comment->date_created;
                $data['sender_fullname'] = UserAccountTb::getFullname($data['user_id']);
                $data['profile_pic'] = UserAccountTb::getProfilePicUrl($data['user_id']);
                $result['status'] = true;
                $pusher = new Pusher(PUSHER_APP_KEY, PUSHER_APP_SECRET, PUSHER_APP_ID);
                $pusher->trigger('my-channel', 'comment-added', $data);
                if($receiver_id != $sender_id){
                    Notification::saveComment($receiver_id,json_encode($data));
                }
            }


    	}
    	echo json_encode($result);
       	$this->view->disable();

    }
}

